<template>
  <el-dialog v-model="drawer" title="新建名片" :append-to-body="true" width="400px" @close="close">
    <el-form ref="ruleRefForm" label-width="80px" :model="form" :rules="rules">
        <el-form-item label="所属组织" prop="company" size="small">
<!--            <el-cascader v-model="form.company" :options="organize" :show-all-levels="false" :props="orgProps" />-->
            <el-select v-model="form.company" placeholder="请选择组织">
                <el-option v-for="item in organize" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
            <el-tooltip effect="dark" content="新建组织" placement="right">
                <el-icon @click="openSetting" size="20px" style="margin-left:5px;cursor:pointer;" color="var(--el-color-primary)"><ele-CirclePlusFilled /></el-icon>
            </el-tooltip>
        </el-form-item>
        <el-form-item label="名片名称" prop="title" size="small">
            <el-input v-model="form.title" maxlength="16" />
        </el-form-item>
    </el-form>
    <template #footer>
        <el-row justify="center">
            <el-button type="primary" size="small" @click="submitForm(ruleRefForm)">提交</el-button>
            <el-button type="default" size="small" @click="cancel(ruleRefForm)">取消</el-button>
        </el-row>
    </template>
  </el-dialog>
</template>

<script>
import {toRefs, computed, reactive, ref, onMounted} from 'vue'
import { useRouter } from 'vue-router'
import {getUserOrgList} from '/@/api/sys/user'

export default {
    name: 'CreateCard',
    props: {
        dialogDrawer: {
            type: Boolean,
            default: false
        }
    },
    emits: ['onSubmit', 'update:dialogDrawer'],
    setup(props, context) {
        const { dialogDrawer } = toRefs(props)
        const drawer = computed({
            get: () => dialogDrawer.value,
            set: val => {
                context.emit('update:dialogDrawer', val)
            }
        })
        const router = useRouter()
        const state = reactive({
            form: {
                company: '',
                title: ''
            },
            orgProps: {
               label: 'name',
               value: 'id'
            },
            rules: {
                company: [{ required: true, message: '请选择所属组织', trigger: 'change' }],
                title: [{ required: true, message: '请输入名片名称', trigger: 'blur' }]
            },
            organize: [{
                id: 1,
                label: 'Level 1',
                isPenultimate: false
            },
            {
                id: 2,
                label: 'Level 2',
                isPenultimate: false,
                children: [{
                    id: 5,
                    label: 'Level 2-1',
                    isPenultimate: false,
                    children: [{
                        id: 9,
                        label: 'Level 2-1-1',
                        isPenultimate: false,
                        children: [{
                            id: 10,
                            label: 'Level 2-1-1-1',
                            isPenultimate: false,
                        }]
                    }]
                },
                {
                    id: 6,
                    label: 'Level 2-2',
                    isPenultimate: false
                }],
            },
            {
                id: 3,
                label: 'Level 3',
                isPenultimate: false,
                children: [{
                    id: 7,
                    label: 'Level 3-1',
                    isPenultimate: false
                },
                {
                    id: 8,
                    label: 'Level 3-2',
                    isPenultimate: false
                }]
            }]
        })
        const ruleRefForm = ref()
        const close = () => {
            context.emit('update:dialogDrawer', false)
        }
        const submitForm = async formEl => {
            if (!formEl) return
            await formEl.validate((valid) => {
                if (valid) {
                    context.emit('onSubmit', state.form)
                    close()
                }
            })
        }
        const cancel = formEl => {
            if (!formEl) return
            formEl.resetFields()
            close()
        }
        const openSetting = () => {
            close()
            return
            // 到新建组织的页面./弹窗.暂无
            router.push({
                path: ''
            })
        }
        const getOrgList = async() => {
            try {
                const res = await getUserOrgList()
                state.organize = res.data
            } catch (e) {
                console.log(e)
            }
        }
        onMounted(() => {
            getOrgList()
        })
        return{
            ...toRefs(state),
            drawer,
            close,
            submitForm,
            cancel,
            ruleRefForm,
            openSetting,
            getOrgList
        }
    }
}
</script>

<style scoped lang="scss">

</style>
